package cn.edu.zafu.bigdata.yeshuchang.wlbc;

import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) {
        int port = 1234; // 服务端端口号
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("Server is running and waiting for client connection...");

            while (true) {
                Socket socket = serverSocket.accept(); // 接受客户端连接
                System.out.println("Client connected.");

                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    System.out.println("Client says: " + inputLine);
                    out.println("Server回复: " + inputLine); // 将客户端的消息回复给客户端
                }
                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
